//
//  PersonalEducationInfoViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/13.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PersonalEducationInfoViewController.h"
#import "PersonalWorkExperienceViewController.h"
#import "AppDelegate.h"
#import "IDSelectViewController.h"
#import "CompanyFirstInfoViewController.h"
#import "CompanySecondInfoViewController.h"
#import "PersonalFirstInfoViewController.h"
#import "CompanyInfoViewController.h"

@interface PersonalEducationInfoViewController ()<TimeSelectPickerViewDelegate, PSSingleRowViewDelegate>
{
    NSString *education;
}
@property (nonatomic, strong) PSCell *schoolCell;
@property (nonatomic, strong) PSCell *professionCell;
@property (nonatomic, strong) PSCell *startTimeCell;
@property (nonatomic, strong) PSCell *endTimeCell;
@property (nonatomic, strong) PSCell *educationCell;
@property (nonatomic, strong) TimeSelectPickerView *startTimePickerView;
@property (nonatomic, strong) TimeSelectPickerView *endTimePickerView;
@property (nonatomic, strong) PSSingleRowView *educationPickerView;
@end

@implementation PersonalEducationInfoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBarHidden = NO;
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIToolsControl resignFirstResponderForTextFieldArray:@[self.schoolCell.textField, self.professionCell.textField]];
    if (self.educationPickerView) {
        self.educationPickerView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    education = @"";
    
    [self initUI];
}
- (void)initUI {
    self.navigationItem.title = @"填写教育经历";
    
    if (self.isRegisterPush) {
//        [self.navigationController popViewControllerAnimated:YES];
    } else {
        UIButton *switchBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, 60*Scale, 44) imageName:nil target:self action:@selector(switchBtnTap) title:@"  我要招人" titleColor:THEMECOLOR_OF_ORANGE];
        switchBtn.titleLabel.font = Font(Font_15);
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:switchBtn];
        self.navigationItem.leftBarButtonItem = leftItem;
    }
    
    UIButton *nextBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36) imageName:nil target:self action:@selector(nextBtnTap) title:@"下一步" titleColor:THEMECOLOR_OF_ORANGE];
    nextBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:nextBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UILabel *topLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, 20*Scale, WIDTH, 30*Scale) text:@"真实完整的个人简历，不但找工作快，还可以赚钱" textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_15];
    topLbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:topLbl];
    
    __weak __typeof(self)weakSelf = self;
    self.startTimePickerView = [[TimeSelectPickerView alloc] initWithTarget:self time:nil timeSelectPickerType:TimeSelectPickerTypeOfWorking dateFormat:nil];
    self.startTimePickerView.hidden = YES;
    self.endTimePickerView = [[TimeSelectPickerView alloc] initWithTarget:self time:nil timeSelectPickerType:TimeSelectPickerTypeOfWorking dateFormat:nil];
    self.endTimePickerView.hidden = YES;
    self.educationPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.educationArray target:self];
    self.educationPickerView.hidden = YES;
    
    self.schoolCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(topLbl)+15*Scale, WIDTH-60*Scale, PSCellHeight) title:@"毕业院校" text:nil placeholder:@"请输入您所毕业院校的名称" cellType:PSCellTypeOfWriteonly];
    [self.view addSubview:self.schoolCell];
    
    self.professionCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.schoolCell), WIDTH-60*Scale, PSCellHeight) title:@"专业" text:nil placeholder:@"请输入" cellType:PSCellTypeOfWriteonly];
    [self.view addSubview:self.professionCell];
    
    self.startTimeCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.professionCell), WIDTH-60*Scale, PSCellHeight) title:@"开始时间" text:nil placeholder:@"年/月/日" cellType:PSCellTypeOfIndicator];
    self.startTimeCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextFieldArray:@[strongSelf.schoolCell.textField, strongSelf.professionCell.textField]];
        strongSelf.startTimePickerView.hidden = NO;
    };
    [self.view addSubview:self.startTimeCell];
    
    self.endTimeCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.startTimeCell), WIDTH-60*Scale, PSCellHeight) title:@"结束时间" text:nil placeholder:@"年/月/日" cellType:PSCellTypeOfIndicator];
    self.endTimeCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextFieldArray:@[strongSelf.schoolCell.textField, strongSelf.professionCell.textField]];
        strongSelf.endTimePickerView.hidden = NO;
    };
    [self.view addSubview:self.endTimeCell];
    
    self.educationCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, EndY(self.endTimeCell), WIDTH-60*Scale, PSCellHeight) title:@"学历" text:nil placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    self.educationCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [UIToolsControl resignFirstResponderForTextFieldArray:@[strongSelf.schoolCell.textField, strongSelf.professionCell.textField]];
        strongSelf.educationPickerView.hidden = NO;
    };
    [self.view addSubview:self.educationCell];
}

- (void)psSingleRowViewCancel:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
}
- (void)psSingleRowViewOk:(PublicCategoryElementModel *)model PSSingleRowView:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
    if (psSingleRowView == self.educationPickerView) {
        self.educationCell.textField.text = model.name;
        education = model.id;
    }
}
- (void)timeSelectPickerViewCancel:(TimeSelectPickerView *)timeSelectPickerView {
    timeSelectPickerView.hidden = YES;
}
- (void)timeSelectPickerViewOK:(NSString *)selectText timeSelectPickerView:(TimeSelectPickerView *)timeSelectPickerView {
    timeSelectPickerView.hidden = YES;
    if (timeSelectPickerView == self.startTimePickerView) {
        self.startTimeCell.textField.text = selectText;
    } else if (timeSelectPickerView == self.endTimePickerView) {
        self.endTimeCell.textField.text = selectText;
    }
}
- (void)nextBtnTap {
    [UIToolsControl resignFirstResponderForTextField:self.schoolCell.textField];
    [UIToolsControl resignFirstResponderForTextField:self.professionCell.textField];
    if (self.schoolCell.textField.text.length>0) {
        if (self.professionCell.textField.text.length>0) {
            if (self.startTimeCell.textField.text.length>0) {
                if (self.endTimeCell.textField.text.length>0) {
                    if (self.educationCell.textField.text.length>0) {
                        if ([DataToolsControl sharedManager].idType == IDTypeOfWorker) { // 工作经历
                            [self upDate];
                        } else { // 直接进首页
                            [self upDate];
                        }
                    } else {
                        SVPHUDError(@"请选择学历");
                    }
                } else {
                    SVPHUDError(@"请选择结束时间");
                }
            } else {
                SVPHUDError(@"请选择开始时间");
            }
        } else {
            SVPHUDError(@"请填写专业");
        }
    } else {
        SVPHUDError(@"请填写学校");
    }
}
- (void)upDate {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:@"" forKey:@"info_id"];
    [dic setObject:[self.startTimeCell.textField.text substringToIndex:7] forKey:@"start_date"];
    [dic setObject:[self.endTimeCell.textField.text substringToIndex:7] forKey:@"end_date"];
    [dic setObject:self.schoolCell.textField.text forKey:@"school"];
    [dic setObject:self.professionCell.textField.text forKey:@"speciality"];
    [dic setObject:education forKey:@"education"];
//    [dic setObject:@"" forKey:@"images"];
    
    [BaseNetManager requestWithMethod:POST path:PersonMine_SaveEduInfo_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
//            SVPHUDSuccess(responseObject[@"msg"]);
            SVPHUDDismiss;
            
//            UD_SetOForK(responseObject[@"data"], isResumeID);
            if ([DataToolsControl sharedManager].idType == IDTypeOfWorker) { // 工作经历
                PersonalWorkExperienceViewController *PWEVC = [[PersonalWorkExperienceViewController alloc] init];
                [self.navigationController pushViewController:PWEVC animated:YES];
            } else {
                [DataToolsControl getMyBaseInfoForWorker:^(BOOL requestFlag) {
                    [SharedApp showViewController:TabBarTypeOfPersonal];
                }];
            }
        } else {
            NSString *str = [NSString stringWithFormat:@"%@", responseObject[@"msg"]];
            SVPHUDError(str);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [UIToolsControl resignFirstResponderForTextField:self.schoolCell.textField];
    [UIToolsControl resignFirstResponderForTextField:self.professionCell.textField];
}
- (void)switchBtnTap {
    NSLog(@"切换角色");
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:@"1" forKey:@"user_role"];
    [BaseNetManager requestWithMethod:POST path:Change_Role_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            UD_SetOForK(@"1", isUserRole);
            
            NSDictionary *data = responseObject[@"data"];
            if (data[@"user_guide"] && [data[@"user_guide"] length] && ([data[@"user_guide"] isEqualToString:@"choose_role"])) { // 用户还没选角色 （选择用户角色）
                IDSelectViewController *IDSVC = [[IDSelectViewController alloc] init];
                IDSVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:IDSVC animated:NO];
            } else {
                if ([data[@"user_guide"] isEqualToString:@"company_add_job"]) { // 添加工作
                    [DataToolsControl getMyBaseInfoForCompany:^(BOOL requestFlag) {
                        [SharedApp showViewController:TabBarTypeOfCompany];
                        UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                        tabBarController.selectedIndex = 1;
                    }];
                } else if ([data[@"user_guide"] isEqualToString:@"company_create"]) { // 招聘者下面没有企业的话就会引导创建
                    CompanyFirstInfoViewController *IDSVC = [[CompanyFirstInfoViewController alloc] init];
                    IDSVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:IDSVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"company_info"]) { // 填写企业信息
                    CompanySecondInfoViewController *IDSVC = [[CompanySecondInfoViewController alloc] init];
                    IDSVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:IDSVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"company_audit"]) { // 企业未认证
                    SVPHUDShow;
                    [DataToolsControl getMyBaseInfoForCompany:^(BOOL requestFlag) {
                        SVPHUDDismiss;
                        [SharedApp showViewController:TabBarTypeOfCompany];
                        UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                        tabBarController.selectedIndex = tabBarController.viewControllers.count-1;
                        RootNavigationController *navigationController = (RootNavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
                        
                        CompanyInfoViewController *CIVC = [[CompanyInfoViewController alloc] init];
                        CIVC.hidesBottomBarWhenPushed = YES;
                        [navigationController pushViewController:CIVC animated:YES];
                    }];
                } else if (([data[@"user_guide"] isEqualToString:@"resume_create"])) { // 创建简历页
                    PersonalFirstInfoViewController *PMRVC = [[PersonalFirstInfoViewController alloc] init];
                    PMRVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:PMRVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"resume_edu"]) { // 完善简历中教育经历
                    if (data[@"is_graduate"] && ([data[@"is_graduate"] integerValue] == 0)) {
                        [DataToolsControl sharedManager].idType = IDTypeOfWorker;
                    } else {
                        [DataToolsControl sharedManager].idType = IDTypeOfFreshGraduate;
                    }
                    
                    PersonalEducationInfoViewController *PMRVC = [[PersonalEducationInfoViewController alloc] init];
                    PMRVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:PMRVC animated:NO];
                } else if ([data[@"user_guide"] isEqualToString:@"resume_work"]) { // 完善简历中工作经历
                    PersonalWorkExperienceViewController *PMRVC = [[PersonalWorkExperienceViewController alloc] init];
                    PMRVC.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:PMRVC animated:NO];
                } else { 
                    [DataToolsControl getMyBaseInfoForCompany:^(BOOL requestFlag) {
                        [SharedApp showViewController:TabBarTypeOfCompany];
                    }];
                }
            }
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)customBackButtonTap {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
